var arg = new Object();

arg.showSidebar = function() {

    $("#sidebar_button_open").click(function() {
        $("#sidebar").show();
        $("#content").width("63%");

    });
    $("#sidebar_button_close").click(function() {
        $("#sidebar").hide();
        $("#content").width('93%');

    });
}

function loginFail(result){
	if(result==1){
		 $("#loginDialog").dialog('open');
	}
}
arg.uploadImages = function() {

    $('#fileInput').uploadify({
        'uploader' : '/js/uploadify/uploadify.swf',
        'script' : '/js/uploadify/uploadify.php',
        'cancelImg' : '/js/uploadify/cancel.png',
        'auto' : false,
        'multi' : true,
        'buttonText' : 'Upload Images'
    });
}

arg.horizontalLayout = (function() {

    $('a.layout_button').click(function() {
        var selected = $(this);
        $('#wrapper').scrollTo(selected.attr('href'), 800);

        return false;
    });
})


arg.menuSlide = (function() {

    $(".menubarListItem").each(function(index, element) {

        var listItem = $(element);
        var sublistChildren = $(element).children(".menubarSublist");

        listItem.mouseenter(function() {
            sublistChildren.fadeIn("fast");

        });

        sublistChildren.mouseleave(function() {
            sublistChildren.fadeOut();

        });
    });
});

arg.loginDialog = function() {

    $("#loginDialog").dialog({
        autoOpen : false,
        resizable : false,
        modal : true,
        buttons : {
            Login : function() {
    			$("#loginForm").submit();
            },
            Cancel : function() {
                $(this).dialog('close');
            }
        }
    });

    $('#login').click(function() {
        $('#loginDialog').dialog('open');
    });

    $("#signupDialog").dialog({
        autoOpen : false,
        resizable : false,
        modal : true,
        buttons: {
            Signup : function() {
          		$("#signupForm").submit();
          	},
            Cancel : function() {
                $(this).dialog('close');
            }
        }
    });

    $('#signup').click(function() {
        $('#signupDialog').dialog('open');
    });
   
    var validator = $("#signupForm").validate({
        rules: {
        	userName: {
                required: true,               
            },
            userSurname: {
                required: true,
            },
            userEmail: {
                required: true,
                email: true,
                remote: "/user/emailAvailable",
            },
            userPassword: {
                required: true,
                minlength: 6,
  //              maxlenght: 20,
             },
             userPassword_confirm: {
                required: true,
                equalTo: "#userPassword",
            },
        } ,      
       
        success: function(label) {
            label.html("&nbsp;").addClass("checked");
        },
       
        submitHandler: function() {
        	$("#hiddenUserName").val($("#userName").val());
        	$("#hiddenUserSurname").val($("#userSurname").val());
        	$("#hiddenUserEmail").val($("#userEmail").val());
        	$("#hiddenUserPassword").val($("#userPassword").val());
        	$("#hiddenUserPassword_confirm").val($("#userPassword_confirm").val());
        	$("#hiddenSignupForm").submit();
        },
    });
    
}
sendNexDestination = function(actualURL, newURL)
{
    XMLHttpRequestObject = false;
    if (!XMLHttpRequestObject) {
        XMLHttpRequestObject = new XMLHttpRequest();
    }

    var url = "http://" + window.location.host + "webDestination?actual=" + actualURL + "&new=" + newURL; //VER
    XMLHttpRequestObject.open("GET", url, false);
    XMLHttpRequestObject.send(null);
}

arg.countCharacters = function(){

    $("#abstractareaArticle").charCounter(220);
    $("#inputTitleArticle").charCounter(70);

}

arg.validator = function(){
    $("#articleForm").validate();
}

arg.subscribe = function() {

    $("#subscribeDialog").dialog({
        autoOpen : false,
        resizable : false,
        modal : true,
        buttons : {
            Subscribe : function() {
                $("#subscribeForm").submit();
            },
            Cancel : function() {
                $(this).dialog('close');
            }
        },
    });

    $('#subscribe').click(function() {
        $('#subscribeDialog').dialog('open');
    });

   
    var validator = $("#subscribeForm").validate({
        rules: {
          
            email: {
                required: true,
                email: true,
            },
        
        } ,     
      
        success: function(label) {
            label.html("&nbsp;").addClass("checked");
        },
      
        submitHandler: function() {
        	$("#hiddenReaderEmail").val($("#readerEmail").val());
        	$("#hiddenArticleId").val($("#articleId").val());
        	$("#hiddenSubscribeForm").submit();
            alert("Subscripcion correcta.	");
        },
      
    });
  
  
}

arg.editProfile = function() {

    $("#editDialog").dialog({
        autoOpen : false,
        resizable : false,
        modal : true,
        buttons : {
            Save : function() {
                $("#editForm").submit();
            },
            Eliminar : function() {
            	$("#delete").submit();
            },
            Cancel : function() {
                $(this).dialog('close');
            }
        },
    });

    $('#editProfile').click(function() {
        $('#editDialog').dialog('open');
        var XMLHttpRequestObject = false;
        if (!XMLHttpRequestObject) {
            XMLHttpRequestObject = new XMLHttpRequest();
        }
        XMLHttpRequestObject.open("POST", "/user/getUserData", false);
        XMLHttpRequestObject.send(null);
        var JSONtext = (XMLHttpRequestObject.responseText);
        var jsonUser = eval('(' + JSONtext + ')');
        
        $("#userName").Value=jsonUser.name;
    });
  

    var validator = $("#editForm").validate({
        rules: {
            name: {
                required: true,              
            },
            lastname: {
                required: true,              
            },
          
            password: {
                required: true,
                minlength: 6,
                maxlenght: 20,
             },
            password_confirm: {
                required: true,
                equalTo: "#password",
            },
        } ,     
      
        success: function(label) {
            label.html("&nbsp;").addClass("checked");
        },
      
       /* submitHandler: function() {
            alert("ok"),
            $('#editDialog').dialog('close');
          
        },*/
      
    });

}

//////////////////

arg.modificarMail = function() {

    
    var validator = $("#modificarMail").validate({
        rules: {
          
            email: {
                required: true,
                email: true,
            },
        
        } ,  
      
//        success: function(label) {
//            label.html("&nbsp;").addClass("checked");
//        },
//      
//        submitHandler: function() {
//            alert("ok"),
//            $('#editDialog').dialog('close');
//          
//        },
      
    });
    


}